﻿using System;

namespace Bouyei.NetworkSniffer.Collections
{
    public class LinkQueue<T>
    {
        #region 定义
        /// <summary>
        /// 队列项
        /// </summary>
        internal sealed class Node
        {
            public T value;
            public int Id = -1;
            public Node Next;
        }

        private int count = 0;
        private Node Head = null;
        private Node temp = null;

        #endregion

        #region 方法

        /// <summary>
        /// 获取队里的个数
        /// </summary>
        public int Count
        {
            get
            {
                return count;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public LinkQueue()
        { }

        /// <summary>
        /// 入队列
        /// </summary>
        /// <param name="value"></param>
        public void Enqueue(T value)
        {
            ++count;
            if (Head == null)
            {
                Head = new Node();
                Head.value = value;
                Head.Id = count;
                temp = Head;
            }
            else
            {
                Node n = new Node();
                n.value = value;
                n.Id = count;
                temp.Next = n;
                temp = temp.Next;
            }
        }

        /// <summary>
        /// 出队列
        /// </summary>
        /// <returns></returns>
        public T Dequeue()
        {
            if (count <= 0 || Head == null)
            {
                return default(T);
            }
            else
            {
                Node t = Head;
                Head = Head.Next;
                --count;
                return t.value;
            }
        }

        /// <summary>
        /// 根据索引位置获取或设置数据项
        /// </summary>
        /// <param name="index">栈位置</param>
        /// <returns></returns>
        public T this[int index]
        {
            get
            {
                if (index < 0 || index > count) return default(T);

                Node n = Head;
                Node p = n;
                Node rt = null;
                int i = 0;

                do
                {
                    if (i == index)
                    {
                        if (i == 0)
                        {
                            Head = n;
                            rt = p;
                            break;
                        }
                        else
                        {
                            rt = p;
                            break;
                        }
                    }
                    p = n;
                    n = n.Next;
                }
                while (i <= index && n != null);
                if (rt != null)
                {
                    --count;
                    return rt.value;
                }
                else
                {
                    return default(T);
                }
            }
            set
            {
                Node n = Head;
                Node p = n;
                int i = 0;

                do
                {
                    if (i == index)
                    {
                        if (i == 0)
                        {
                            Head = n;
                            p.value = value;
                            break;
                        }
                        else
                        {
                            p.value = value;
                            break;
                        }
                    }
                    p = n;
                    n = n.Next;
                }
                while (i <= index && n != null);
            }
        }

        /// <summary>
        /// 清除队列所有数据
        /// </summary>
        public void Clear()
        {
            while (Head != null)
            {
                Head = Head.Next;
            }
            this.Head = null;
            this.temp = null;
            count = 0;
        }

        #endregion

    }
}
